//
//  QDSliderController.m
//  simulateQuickDoctor
//
//  Created by 梁家伟 on 17/3/22.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDSliderController.h"

@interface QDSliderController ()
@property(nonatomic,strong)UITapGestureRecognizer* tap;
@end

@implementation QDSliderController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupContainer];
    
    //初始化点击手势
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backToLeft:)];
    
    _tap = tap;
}

-(instancetype)initWithLeftVC:(UIViewController*)leftVC andRightVC:(UIViewController*)rightVC{
    
    if(self = [super init]){
        _leftVC = leftVC;
        _rightVC = rightVC;
        
        //添加手势
        
        UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(slide:)];
        
        [_rightVC.view addGestureRecognizer:pan];
        
    }
    
    return self;
}





/**
 左侧菜单划出
 */
-(void)slideToRight{
    CGFloat width = self.view.bounds.size.width;
    
    [UIView animateWithDuration:0.3 animations:^{
        _rightVC.view.transform =  CGAffineTransformMakeTranslation(width - 96, 0);
    } completion:^(BOOL finished) {
        
        [_rightVC.view addGestureRecognizer:_tap];
        
    }];
    
    
}


/**
 实现向右侧滑效果

 @param pan 拖拽手势
 */
-(void)slide:(UIPanGestureRecognizer*)pan{
    //如果不是首页，不能使用侧滑效果
    if(![self isTopViewController]){
        return;
    }
    
    CGFloat width = self.view.bounds.size.width;

    CGPoint offset = [pan translationInView:_rightVC.view];
    
    [pan setTranslation:CGPointZero inView:_rightVC.view];
    
    
    switch (pan.state) {
        case UIGestureRecognizerStateBegan:
        case UIGestureRecognizerStateChanged:
            
            
            if(offset.x + _rightVC.view.frame.origin.x < 0){
                _rightVC.view.transform = CGAffineTransformIdentity;
                return;
            }
            _rightVC.view.transform = CGAffineTransformTranslate(_rightVC.view.transform, offset.x, 0);
            
            break;
            
            
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateFailed:
        case UIGestureRecognizerStateCancelled:
            
            [self moveToRightWithWidth:width];
            
            break;
        default:
            break;
    }
    
}



/**
 判断是否为首页

 @return 是否为首页
 */
-(BOOL)isTopViewController{
    UITabBarController* RightVC = (UITabBarController*)_rightVC;
    UINavigationController* nav = RightVC.selectedViewController;
    
    if(nav.viewControllers.count > 1){
        return NO;
    }
    return YES;
}

/**
 拖拽松手时候做的事情

 @param width 当前根视图的宽度
 */
-(void)moveToRightWithWidth:(CGFloat)width{
    //超过屏幕一半的时候松手
    if(_rightVC.view.frame.origin.x > width * 0.5){
        
        [UIView animateWithDuration:0.3 animations:^{
            _rightVC.view.transform =  CGAffineTransformMakeTranslation(width - 96, 0);
        } completion:^(BOOL finished) {
            
            [_rightVC.view addGestureRecognizer:_tap];
            
        }];
        
    }else{
        [self resetTransform];
    }
}


/**
 返回初始位置
 */
-(void)resetTransform{
    
    [UIView animateWithDuration:0.3 animations:^{
        _rightVC.view.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        
        //移除tap手势
        
        [_rightVC.view removeGestureRecognizer:_tap];
        
    }];
    
}


/**
 左侧菜单收回去

 @param tap 点击手势
 */
-(void)backToLeft:(UITapGestureRecognizer*)tap{
    [self resetTransform];
}

/**
 设置容器控制器
 */
-(void)setupContainer{
    
    //先添加到父容器中
    [self addChildViewController:_leftVC];
    [self addChildViewController:_rightVC];
    
    //然后添加视图到父视图中
    [self.view addSubview:_leftVC.view];
    [self.view addSubview:_rightVC.view];
    
    //调用下didMoveTo方法
    [_leftVC didMoveToParentViewController:self];
    [_rightVC didMoveToParentViewController:self];
}


-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
